<!-- 设备管理 -->
<template>
  <MainSlot v-loading="loading">
    <template v-slot:searchForm>
      <SearchForm :formItemList="formItemList" @search="search"></SearchForm>
    </template>
    <template v-slot:tab>
      <EnumSlot @handleEvent="getEnumEvent" :selectIndex="selectIndex" :enumList="enumList"></EnumSlot>
    </template>
    <template v-slot:btn>
      <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
    </template>
    <template v-slot:table>
      <TableData v-if="resetTable" :config="table_config"
        :tableThead="searchFormData.type == 'IOT' ? tableTheadIot : tableTheadVirtual" :dataList="dataList"
        :loading="loading" @selection-change="selectionChange" @sort-change="sortChange">
        <template v-slot:enable="{ row }">
          <span v-if="row.enable">启用</span>
          <span v-if="!row.enable">停用</span>
        </template>
      </TableData>
    </template>
    <template v-slot:pagination>
      <Pagination @size-Change="handleSizeChange" :size="size" @current-change="handleCurrentChange" :page="page"
        :total="total">
      </Pagination>
    </template>
    <template v-slot:dialogModule>
      <AddEdit :title="'设备'" @closeDialog="closeDialog" :addEditFlag="addEditFlag" :markType="searchFormData.type"
        :dialogVisible="dialogVisible" :data="formData">
      </AddEdit>
    </template>
  </MainSlot>
</template>
<script>
import MainSlot from "@/components/MainSlot";
import Pagination from '@/components/Pagination';
import SearchForm from '@/components/SearchForm';
import TableData from '@/components/TableData';
import BtnSlot from '@/components/BtnSlot';
import EnumSlot from '@/components/EnumSlot';
import AddEdit from './components/addEdit.vue';
import equimentManagement from '@/api/basicInformation/equimentManagement.js'

export default {
  components: { MainSlot, Pagination, SearchForm, TableData, BtnSlot, EnumSlot, AddEdit },
  data() {
    return {
      resetTable: true,
      loading: false,
      dialogVisible: false,
      addEditFlag: 'add',
      formData: {},
      page: 1,
      size: 10,
      total: 10,
      selectIndex: 0,
      selectionList: [],
      searchFormData: {
        type: 'IOT',
        deviceName: ''
      },
      btnList: ['add', 'delete'],
      enumList: [{
        label: "物联设备",
        type: 'IOT'
      },
      {
        label: "虚拟设备",
        type: 'VIRTUAL'
      }],
      dataList: [],
      tableTheadIot: [
        { isShow: true, label: '设备名称', prop: 'deviceName' },
        { isShow: true, label: '设备编号', prop: 'deviceNo' },
        { isShow: true, label: 'RTU编号', prop: 'rtuNo' },
        { isShow: true, label: '状态', slotName: 'enable' }
      ],
      tableTheadVirtual: [
        { isShow: true, label: '设备名称', prop: 'deviceName' }
      ],
      // 配置项
      table_config: {
        isCheckBox: true,
        isOperation: true,
        // 表格操作列
        operation: {
          label: '操作',
          width: '200',
          cols: [
            {
              label: '编辑',
              type: 'primary',
              flag: 'edit',
              handleRow: this.handleRow
            },
            {
              label: '删除',
              type: 'danger',
              flag: 'delete',
              handleRow: this.handleRow
            }
          ]
        },
      },
      formItemList: [{
        label: '设备名称',
        type: 'input',
        param: 'deviceName',
        isShow: true
      }]
    }
  },
  mounted() {
    this.queryPageByCondition();
  },
  methods: {
    queryPageByCondition() {
      let params = {
        page: this.page,
        size: this.size,
        entity: {
          ...this.searchFormData,
          organizationNo: sessionStorage.organizationNo
        }
      }
      equimentManagement.queryPageByCondition({ ...params }).then(res => {
        if (res.code == 1) {
          this.dataList = res.data.content || [];
          this.total = res.data.totalElements;
          if (res.data.totalPages && this.page > res.data.totalPages) {
            this.page = res.data.totalPages;
            this.queryPageByCondition();
          }
        } else {
          this.$message.warning(res.msg || '分页查询失败')
        }
      }).catch(err => {
        console.log(err)
      })
    },
    search(data) {
      this.page = 1;
      this.searchFormData = { ...this.searchFormData, ...data };
      this.queryPageByCondition();
    },
    handleCurrentChange(page) {
      console.log(page)
      this.page = page
      this.queryPageByCondition();
    },

    handleSizeChange(size) {
      this.size = size;
      this.queryPageByCondition();
    },
    // 操作方法
    handleRow(rowData, item) {
      console.log(rowData, item);
      if (item.flag == 'delete') {
        this.delList([rowData]);
      } else if (item.flag == 'edit') {
        this.dialogVisible = true;
        this.formData = JSON.parse(JSON.stringify(rowData))
      }
      this.addEditFlag = item.flag
    },

    delList(deleteList = [], key = 'deviceNo') {
      let noList = []
      deleteList.forEach(item => {
        noList.push(item[key])
      })
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await equimentManagement.delListFarmingTypelist({ noList });
        this.$message[res.code == 1 ? 'success' : 'warning'](res.code == 1 ? '操作成功' : res.msg || '操作失败')
        this.queryPageByCondition();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 多选方法
    selectionChange(val) {
      console.log(val);
      this.selectionList = val || []
    },
    // 排序方法
    sortChange(val) {
      console.log(val);
    },
    //获取选中Enum
    getEnumEvent(enumInfo) {
      this.page = 1;
      this.resetTable = false;
      this.$nextTick(() => {
        this.selectIndex = enumInfo.index || 0;
        this.$set(this.searchFormData, 'type', enumInfo.data.type);
        this.queryPageByCondition();
        this.resetTable = true;
      });
    },
    //获取选中Btn
    getBtnEvent(flag) {
      console.log(flag)
      if (flag == 'add') {
        this.dialogVisible = true;
      } else if (flag == 'delete') {
        if (this.selectionList.length > 0) {
          this.delList(this.selectionList)
        } else {
          this.$message.warning('请先选择表单数据项后再删除！')
        }
      }
      this.addEditFlag = flag;
    },
    //关闭弹窗
    closeDialog() {
      this.dialogVisible = false;
      this.queryPageByCondition();
    },
  }
};
</script>
<style lang="scss" scoped>
.pointer {
  cursor: pointer;
}
</style>